﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Config.Enum;
using Entity;
using PaymentBLL;

namespace WebTriple7.Class
{
    public class GateWayPayment
    {
        private GateWay GateWayId { get; set; }
        private uspPaymentGatewaySelectResult GateWayInfo { get; set; }
        
        public GateWayPayment(GateWay gate)
        {
            this.GateWayId = gate;
            var payment=new CtrPaymentGateway();
            this.GateWayInfo = payment.SelectPaymentGetway((int)gate);
        }
        public uspPaymentGatewaySelectResult GetGateWayInfo()
        {
            return this.GateWayInfo;
        }
        public ResultPayment<int> GetBlance()
        {
            var rel = new ResultPayment<int>();
            rel.Status = -10;
            rel.Code = null;
            rel.Data = -1;
            rel.Mess = "Không tồn tại phương thức thanh toán";
            switch (this.GateWayId)
            {
                case GateWay.Ebank:
                    var payment = new ApiGoPayment();
                    var blance = payment.GetBalance();
                    if (blance == null || blance.TotalVcoin<0)
                    {
                        rel.Status = 0;
                        rel.Code = -1;
                        rel.Data = -1;
                        rel.Mess = "Lỗi kết nối với Ebank";
                    }
                    else
                    {
                        rel.Status = 1;
                        rel.Code = blance.TotalVcoinPayment;
                        rel.Data = blance.TotalVcoin;
                        rel.Mess = "Lấy Vcoin thành công";
                    }
                    break;

            }
            return rel;
        }
        public ResultPayment<string> DeductMoney(int amount, int itemCode, long transId)
        {
            var rel = new ResultPayment<string>();
            rel.Status = -10;
            rel.Code =null;
            rel.Data = "";
            rel.Mess = "Không tồn tại phương thức thanh toán";
            switch (this.GateWayId)
            {
                case GateWay.Ebank:
                    var payment = new ApiGoPayment();
                    var ret = payment.DeductVcoin(amount, itemCode, transId, "");
                    rel.Status = ret._code >= 0 ? 1 : 0;
                    rel.Code = ret._code;
                    rel.Data = Newtonsoft.Json.JsonConvert.SerializeObject(ret);
                    rel.Mess = ret._message;
                    break;
            }
            return rel;
        }

        public ResultPayment<string> InputMoney(int amount, long transId)
        {
            var rel = new ResultPayment<string>();
            rel.Status = -10;
            rel.Code = null;
            rel.Data = "";
            rel.Mess = "Không tồn tại phương thức thanh toán";
            switch (this.GateWayId)
            {
                case GateWay.Ebank:
                    var payment = new ApiGoPayment();
                    var vnd =(Int32)(GateWayInfo.VndOut*(amount/GateWayInfo.ReceiveAmount));
                    var ret = payment.InputMoneyBetting(vnd,transId);
                    rel.Status = ret._code >= 0 ? 1 : 0;
                    rel.Code = ret._code;
                    rel.Data = Newtonsoft.Json.JsonConvert.SerializeObject(ret);
                    rel.Mess = ret._message;
                    break;
            }
            return rel;
        }
    }
    /// <summary>
    /// kết quả trả về của các phương thức thanh toán
    /// </summary>
    public class ResultPayment<T>
    {
        public int Status { get; set; }
        public int? Code { get; set; }
        public T Data { get; set; }
        public string Mess { get; set; }
    }
}